<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\MessageModel;
use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

class MessageController extends AdminBaseController
{
    //投诉列表
    public function index()
    {
        $this->view->admin_all = $admin = UserModel::where('user_type', 1)->where('user_status', 'neq', 0)->select();

        $this->assign('mag_type', config('message_type'));

        $this->view->admin_id = $admin_id = input('admin_id', cmf_get_current_admin_id());
        $this->view->msg_type = $msg_type = input('msg_type');
        $where = [];

        if ($admin_id) $where['admin_id'] = $admin_id;
        if ($msg_type) $where['state'] = $msg_type;
        $this->view->mas_list = $mas_list = MessageModel::where($where)->order('msg_id', 'desc')->paginate(20, false, [
            'query' => ['admin_id' => $admin_id, 'msg_type' => $msg_type]
        ]);


        return $this->fetch();
    }

    //修改状态 客服是自己 或者自己是超级管理员 才可以进入下一步
    public function up()
    {
        $id = input('msg_id/n');

        $msg = MessageModel::where('msg_id', $id)->find();


        //验证客服是不是自己 和超级管理员
        if ($msg['admin_id'] != cmf_get_current_admin_id() && cmf_get_current_admin_id() != 1) {
            $this->error('客服是自己或者超级管理员才可以操作');
        }
        $update_data = [];
        if ($state = input('state/n')) {
            //修改投诉处理状态
            $state++;
            $state = min($state, 3);
            $update_data['state'] = $state;
        }
        elseif ($state = input('result/s')) {
            $update_data['result'] = $state;
        }

        //验证修改的有内容没
        if(!$update_data) $this->error('没有修改的内容');

        $status = Db::name('message')->where('msg_id', $id)->update($update_data);
        if($status) $this->success('操作成功');
        else $this->error('操作失败');

    }

    //获取消息数量
    public function getMsgCount()
    {
       $count = MessageModel::where('admin_id',cmf_get_current_admin_id())/*登入着的消息*/
           ->where('state',1)/*待处理*/
           ->count();
        $this->success('','',['count'=>$count]);
    }
}
